//
//  UserOrderLogisticsOrderInfo.m
//  SmallElephant
//
//  Created by Faith on 2024/11/24.
//

#import "UserOrderLogisticsOrderInfo.h"

@interface UserOrderLogisticsOrderInfo ()

/// 店铺名称
@property (nonatomic, strong) UILabel *shopName;

/// 商品图片
@property (nonatomic, strong) UIImageView *coverView;

/// 商品名称
@property (nonatomic, strong) UILabel *productName;

/// 规格信息
@property (nonatomic, strong) UILabel *specInfo;

/// 订单金额
@property (nonatomic, strong) UILabel *orderAmount;

/// 商品数量
@property (nonatomic, strong) UILabel *productNumber;

@end

@implementation UserOrderLogisticsOrderInfo

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupViews];
    }
    return self;
}

- (void)setupViews {
    
    self.backgroundColor = [UIColor whiteColor];
    [self applyStyle:@"padding: 15px;"];
    
    // 按钮操作组
    UIView *shopView = [[UIView alloc] init];
    [self addSubview:shopView style:@"display: flex; flex-direction: row; align-items: center;"];
    
    // 店铺名称
    _shopName = [[UILabel alloc] init];
    [shopView addSubview:_shopName style:@"display: flex; height: 30px; padding-left:10px; font-size: 15px; color: #0D0D0D;"];
    
    // 订单信息
    UIView *orderInfo = [[UIView alloc] init];
    [self addSubview:orderInfo style:@"display: flex; flex-direction: row; margin-top: 5px;"];
    // 商品封面
    _coverView = [[UIImageView alloc] init];
    [orderInfo addSubview:_coverView style:@"display: flex; width: 80px; height: 80px; border-radius: 5px;"];
    
    // 商品标题和规格
    UIView *infoView = [[UIView alloc] init];
    [orderInfo addSubview:infoView style:@"display: flex; flex: 1; flex-direction: column; margin-left: 8px; "];
    // 商品标题
    _productName = [[UILabel alloc] init];
    _productName.numberOfLines = 0;
    [infoView addSubview:_productName style:@"font-size: 14px; color: #0D0D0D; padding: 1px;"];
    // 商品规格
    _specInfo = [[UILabel alloc] init];
    [infoView addSubview:_specInfo style:@"margin-top: 5px; font-size: 12px; color: #666666;"];
    
    // 订单金额和数量
    UIView *orderAmountAndNumber = [[UIView alloc] init];
    [orderInfo addSubview:orderAmountAndNumber style:@"display: flex; flex-direction: column; margin-top: 5px; margin-left: 10px; align-items: flex-end;"];
    
    // 订单金额
    _orderAmount = [[UILabel alloc] init];
    _orderAmount.font = [UIFont systemFontOfSize:FONT_SIZE(12) weight:UIFontWeightBold];
    [orderAmountAndNumber addSubview:_orderAmount style:@"display: flex; color: #333333;"];
    // 商品数量
    _productNumber = [[UILabel alloc] init];
    [orderAmountAndNumber addSubview:_productNumber style:@"display: flex; width: 100%; text-align: right; margin-top: 5px; font-size: 13px; color: #666666;"];
    
    // 按钮操作组
    UIView *operationButton = [[UIView alloc] init];
    [self addSubview:operationButton style:@"display: flex; flex-direction: row; justify-content: center; align-items: center; margin-top: 10px;"];
    
    UIButton *btnCancel = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnCancel setTitle:@"查看全部订单信息 >" forState:UIControlStateNormal];
    [operationButton addSubview:btnCancel style:@"display: flex; height: 30px; padding-left:10px; font-size: 12px; color: #888888;"];
    
}

/// 设置订单数据
- (void)setData:(NSDictionary *)data {
    self.shopName.text = [NSString stringWithFormat:@"%@", data[@"shopName"]];;
    [self.coverView setImageUrl:data[@"cover"]];
    self.productName.text = [NSString stringWithFormat:@"%@", data[@"title"]];
//    [self.productName setNeedsLayout];
//    [self.productName layoutIfNeeded];
    self.specInfo.text = [data[@"spec"] componentsJoinedByString:@", "];
    // 格式化价格
    NSString *orderAmountString = [NSString stringWithFormat:@"%.2f", [data[@"amount"] floatValue]];
    NSRange dotRange = [orderAmountString rangeOfString:@"."];
    NSMutableAttributedString *orderAmountAttributeString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"¥%@", orderAmountString]];
    [orderAmountAttributeString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:FONT_SIZE(18)] range:NSMakeRange(1, dotRange.location)];
    self.orderAmount.attributedText = orderAmountAttributeString;
    // 设置数量
    self.productNumber.text = [NSString stringWithFormat:@"共%@件", data[@"number"]];
}

@end
